/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.scripting;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Level;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

import com.google.code.cubeirc.base.Base;
import com.google.code.cubeirc.debug.DebuggerQueue;




public final class ScriptWatcher extends Base {

	//private Map<String, String> keys  = new HashMap<String, String>();

	public ScriptWatcher(String name) {
		super(name);
		File f = new File("./scripts/");
		if(!f.isDirectory()) { f.mkdirs(); }
		try {			
			register(".\\scripts\\");			
		} catch (IOException e) {
			org.apache.log4j.Logger fLog = org.apache.log4j.Logger.getLogger("log.script.scriptwatcher");
			fLog.error("ScriptWatcher was unable to initialize.", e);
		}
	}

	private void register(String dir) throws IOException {

		File d = new File(dir);
		String[] listf = d.list();
		JNotify.addWatch(dir, JNotify.FILE_CREATED |  JNotify.FILE_DELETED |  JNotify.FILE_MODIFIED|  JNotify.FILE_RENAMED, true, new ScriptListener() );

		for (int i=0;i<listf.length;i++)
		{
			File f = new File(dir+listf[i]); 	
			
			if (f.isDirectory() || ( !f.toString().contains(".js") && !f.toString().contains(".rb") && !f.toString().contains(".py") && !f.toString().contains(".lua")))
				continue;
			ScriptManager.addScript(f);			
			DebuggerQueue.addDebug(this, Level.INFO,"Adding script %s",f.getName());
		}
	}

	//TODO: Fix 
	private class ScriptListener implements JNotifyListener {

		@Override
		public void fileRenamed(int wd, String rootPath, String oldName, String newName)
		{
		
		}

		@Override
		public void fileModified(int wd, String rootPath, String name)
		{
			if (CheckExt(name))
			{
				DebuggerQueue.addDebug(this, Level.INFO,"Script modified: %s",rootPath+name);
			    ScriptManager.modifyScript(new File(name));
			}
		}

		@Override
		public void fileDeleted(int wd, String rootPath, String name)
		{		if (CheckExt(name))
		{
			DebuggerQueue.addDebug(this, Level.INFO,"Script deleted: %s",rootPath+name);
			ScriptManager.removeScript(new File(name));
		}
		}

		@Override
		public void fileCreated(int wd, String rootPath, String name)
		{
			if (CheckExt(name))
			{
				DebuggerQueue.addDebug(this, Level.INFO,"Script created: %s",rootPath+name);
				ScriptManager.addScript(new File(name));										
			}
		}
	};

	private boolean CheckExt(String f)
	{
		if (!f.contains(".js") && !f.contains(".rb") && !f.contains(".py") && !f.contains(".lua"))
			return false;
			else
				return true;
	}
}
